<?
/**
*  任务管理
*  2014/5/11
*/
include("database.php");

include("session.php");
require("ajax_base.php");

$action = $_GET['action'];
$table_name = "qc_task";

if($action == "del") {
	$db->doDelete($table_name, 'id='.$_GET['id']); 
	$db->doDelete("qc_call", 'task_id='.$_GET['id']); 
	$db->doDelete("qc_task_run", 'task_id='.$_GET['id']); 
	echo "ok";

} else if($action == "list") {
	$condition = "1=1";
	if(isset($_GET['task_status'])) {
		$condition .= " and task_status=".$_GET['task_status'];
	}
	if(isset($_GET['call_type'])) {
		$condition .= " and call_type=".$_GET['call_type'];
	}
	if(isset($_GET['task_name'])) {
		$condition .= " and task_name like '%".$_GET['task_name']."%'";
	}
	if(isset($_GET['callout_number'])) {
		$condition .= " and callout_number like '%".$_GET['callout_number']."%'";
	}
	if(isset($_GET['user_name'])) {
		$condition .= " and b.user_name = '".$_GET['user_name']."'";
	}
	if($_SESSION['user_flag'] == "user") {
		$condition .= " and user_id = ".$_SESSION['user_id'];
	}
	$sql = "select a.id,task_name, a.callout_number, call_type, '' as voice_content, 
	call_count,success_count, fail_count, create_time, start_time, task_status, b.user_name, task_desc, voice_file, voice_text
	from $table_name a Left Join qc_user b On a.user_id=b.id where $condition order by a.id desc"; 
	echo to_grid_json($db, $sql, $dic_data, array("client_name"=>"blue_color_mapper",
		"callout_number"=>"red_color_mapper", 
		"success_count"=>"blue_color_mapper",
		"fail_count"=>"red_color_mapper",
		"voice_content"=>"voice_content_mapper",
		"task_status"=>"task_status_mapper"));

} else if($action == "get") {
	$sql = "select * from $table_name where id=".$_GET['id']; 
	$arr = $db->getOneRecordArray($sql);
	echo array_to_json($arr);

} else if($action == "add") {
	unset($_POST['id']);
	$_POST['user_id'] = $_SESSION['user_id'];
	$_POST['create_time'] = date("Y-m-d H:i:s");
	$_POST['select_key_desc'] = handle_select_key_desc($_POST['select_key_desc']);
	if(empty($_POST['appt_time'])) {
		unset($_POST['appt_time']); // 未设置预约时间
	}
	$db->doInsert($table_name, $_POST);
	echo 'ok';

} else if($action == "edit") {
	$id = $_POST['id'];
	unset($_POST['id']);
	$_POST['select_key_desc'] = handle_select_key_desc($_POST['select_key_desc']);
	if(empty($_POST['appt_time'])) {
		unset($_POST['appt_time']); // 清除预约时间
		$db->query('update '.$table_name.' set appt_time=NULL where id='.$id);
	}
	$db->doUpdate($table_name, $_POST, 'id='.$id);
	echo 'ok';

} else if($action == "start") {
	/*if($_SESSION['user_flag'] == "user") { // 普通用户，只能启动一个任务
		if($db->getOneRecord("select count(id) as cnt from qc_task where task_status=1 and id<>".$_GET['id']) != null) {
			echo 'another_task_is_running';
			exit;
		}
	}*/
	if($_SESSION['user_flag'] == "user") { // 普通用户，最后启动10个任务
		$cnt = $db->getOneValue("select count(id) as cnt from qc_task where task_status=1 and user_id = ".$_SESSION['user_id']);
		if($cnt == null) { 
			$cnt = 0;
		}
		if(intval($cnt) >= 10) {
			echo 'task_is_running_over_max';
			exit;
		}
	}
	if($db->getOneRecord("select id from qc_task_run where task_id=".$_GET['id']) != null) {
		echo 'this_task_is_running';
		exit;
	}
	$db->query("insert into qc_task_run(task_id) values(".$_GET['id'].")"); 
	$db->query("update qc_task set task_status=1, start_time='".date("Y-m-d H:i:s")."' where id=".$_GET['id']); 
	echo "ok";

} else if($action == "get_active_task_count") {
	echo $db->getOneValue("select count(id) from qc_task where task_status<>3");

} else if($action == "stop") {
	$db->query("delete from qc_task_run where task_id=".$_GET['id']); 
	$db->query("update qc_task set task_status=3 where id=".$_GET['id']); 
	echo "ok";

} else if($action == "set_pause") {
	$db->query("update qc_task_run set run_status=0 where task_id=".$_GET['id']); // 任务队列表标识暂停
	$db->query("update qc_task set task_status=2 where id=".$_GET['id']);  // 任务主表标识暂停
	echo "ok";

} else if($action == "set_continue") {
	$db->query("update qc_task_run set run_status=1 where task_id=".$_GET['id']); // 任务队列表标识正常
	$db->query("update qc_task set task_status=1 where id=".$_GET['id']);  // 任务主表标识正常
	echo "ok";

} else if($action == "get_select_options") {
	$sql = "select id,task_name from qc_task where 1=1";
	if($_SESSION['user_flag'] == "user") {
		$sql .= " and user_id=".$_SESSION['user_id'];
	}
	$db->query($sql); 
	$arr = array();
	while($db->nextRecord()) {
		$arr[] = $db->f("id").",".$db->f("task_name");
	}
	echo join("$", $arr);
}


function voice_content_mapper($v, $arr) {
	if($arr['call_type'] == 1) { 
		if( !empty($arr['voice_file']) ) {
			return "<a href=\"javascript:show_voice(\\'".$arr['voice_file']."\\')\">查看录音</a>";
		}
	}
	return $arr['voice_text'];
}

function task_status_mapper($v, $arr) {
	if($v == 0) {
		return "未启动";
	} else if($v == 1) { 
		return "<font color=green>执行中</font> (<a href=\"javascript:pause_task(".$arr['id'].")\">暂停</a>)";
	} else if($v == 2) { 
		return "<font color=red>暂停中</font> (<a href=\"javascript:continue_task(".$arr['id'].")\">恢复</a>)";
	} else if($v == 3) { 
		return "<font color=gray>已结束</font>";
	}
}

function handle_select_key_desc($key_desc) {
	// 替换按键注解中可能出现的全角字符; 2014/12/15
	$key_desc = str_replace("：", ":", $key_desc);
	$key_desc = str_replace("，", ",", $key_desc);
	return $key_desc;
}

?>